package com.betterwood.dragon.dao.enhances.impl;

import com.betterwood.dragon.dao.common.MongoDBUtils;
import com.betterwood.dragon.dao.enhances.CustomeChangeRepo;
import com.betterwood.dragon.dao.enhances.CustomeRecordRepo;
import com.betterwood.dragon.dao.enhances.UserMongoRepository;
import com.betterwood.dragon.entity.Change;
import com.betterwood.dragon.entity.Record;
import com.betterwood.dragon.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import cn.hutool.core.util.StrUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.regex.Pattern;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * @author zhengyanhua
 * @create 2021-03-18 13:59
 */
public class CustomeChangeRepoImpl implements CustomeChangeRepo {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Change> listByQuery(Change change) {
        Query query = new Query();
        if (StrUtil.isNotBlank(change.getSiteName())) {
            query.addCriteria(where("siteNo").is(change.getSiteName()));
        }

        if (StrUtil.isNotBlank(change.getSkuName()) && !StrUtil.equals(change.getSkuName(), "EMPTY")) {
            Pattern pattern = Pattern.compile("^.*" + change.getSkuName() + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(where("skuName").regex(pattern));
        }

        return mongoTemplate.find(query, Change.class);
    }
}
